﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BusinessObjects.BO_GetObjects;
using System.Data;
using System.Data.SqlTypes;
using DigiLib;
using System.Xml;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class RetheServices : System.Web.Services.WebService
{
    public RetheServices()
    {
    }

    [WebMethod]
    public List<Categories> GetChildrenCategory(int parent_id)
    {
        try
        {
            List<Categories> listCategories = new List<Categories>();
            DataTable objDT = BO_portal_categories.SelectByParent(parent_id, 1);
            Categories category = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    category = new Categories();
                    category.CATEGORY_ID = row["CATEGORY_ID"].ToString();
                    category.CATEGORY_NAME = row["FULL_CATEGORY_NAME"].ToString();
                    category.ANCHOR_TEXT = "/c" + row["CATEGORY_ID"].ToString() + "/" + DigiLib.StringUtils.RemoveUnicode(row["FULL_CATEGORY_NAME"].ToString(), "-") + ".htm";
                    listCategories.Add(category);
                }
            }
            else
            {
                listCategories = null;
            }
            return listCategories;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<Partners> LoadRightPartnerLogo(int module_id)
    {
        try
        {
            List<Partners> listPartners = new List<Partners>();
            DataTable objDT = BO_portal_partners.GetByModuleID(module_id);
            Partners partner = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    partner = new Partners();
                    partner.PARTNER_NAME = row["PARTNER_NAME"].ToString();
                    partner.LOGO_PATH = row["LOGO_PATH"].ToString();
                    partner.WEBSITE = row["WEBSITE"].ToString();
                    listPartners.Add(partner);
                }
            }
            else
            {
                listPartners = null;
            }
            return listPartners;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<Advertising> LoadRightADS(int module_id)
    {
        try
        {
            List<Advertising> listADS = new List<Advertising>();
            DataTable objDT = BO_portal_ads_pictures.GetByModuleID(module_id);
            Advertising ads = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    ads = new Advertising();
                    ads.PICTURE_ID = row["PICTURE_ID"].ToString();
                    ads.PICTURE_NAME = row["PICTURE_NAME"].ToString();
                    ads.PICTURE_PATH = row["PICTURE_PATH"].ToString();
                    ads.TARGET_LINK = row["TARGET_LINK"].ToString();
                    listADS.Add(ads);
                }
            }
            else
            {
                listADS = null;
            }
            return listADS;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<Keywords> GetHotKeywords()
    {
        try
        {
            List<Keywords> listKeywords = new List<Keywords>();
            DataTable objDT = BO_portal_keywords.SelectHot();
            Keywords keyword = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    keyword = new Keywords();
                    keyword.TITLE = row["TITLE"].ToString();
                    keyword.CATEGORY_ID = row["CATEGORY_ID"].ToString();
                    listKeywords.Add(keyword);
                }
            }
            else
            {
                listKeywords = null;
            }
            return listKeywords;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<Deals> GetHighLightsDeals(int rows)
    {
        try
        {
            List<Deals> listDeals = new List<Deals>();
            DataTable objDT = BO_portal_deals.SelectTop(rows, 1);
            Deals deal = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    deal = new Deals();
                    deal.DEAL_ID = Convert.ToInt32(row["DEAL_ID"]);
                    deal.CATEGORY_ID = Convert.ToInt32(row["CATEGORY_ID"]);
                    deal.TOTAL_VIEW = Convert.ToInt32(row["TOTAL_VIEW"]);
                    deal.PUBLISH_DATE = DigiLib.DateTimeUtils.dd_MM_yyyy(Convert.ToDateTime(row["PUBLISH_DATE"]));
                    deal.DESCRIPTION = DigiLib.StringUtils.Substring(row["DESCRIPTION"].ToString(), 0, 60);
                    deal.SALE_PRICE = Convert.ToDecimal(row["SALE_PRICE"]).ToString("0,#,##");
                    deal.ORIGIN_PRICE = Convert.ToDecimal(row["ORIGIN_PRICE"]).ToString("0,#,##");
                    deal.DOWN_PRICE = Convert.ToDecimal(Convert.ToInt32(row["ORIGIN_PRICE"]) - Convert.ToInt32(row["SALE_PRICE"])).ToString("0,#,##");
                    deal.DOWN_PRICE_IN_PERCENT = Convert.ToDecimal(row["DOWN_PRICE_IN_PERCENT"]);
                    deal.THUMB_IMG = row["THUMB_IMG"].ToString();
                    deal.TITLE = DigiLib.StringUtils.Substring(row["TITLE"].ToString(), 0, 55);
                    deal.TOOLTIP = row["TITLE"].ToString();
                    deal.LINK = "/khuyen-mai/" + DigiLib.StringUtils.RemoveUnicode(row["TITLE"].ToString(), "-") + "-c" + deal.CATEGORY_ID + "a" + deal.DEAL_ID + ".htm";
                    listDeals.Add(deal);
                }
            }
            else
            {
                listDeals = null;
            }
            return listDeals;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<Deals> GetTopDealsHome(int rows)
    {
        try
        {
            List<Deals> listDeals = new List<Deals>();
            DataTable objDT = BO_portal_deals.SelectTop(rows, 1);
            Deals deal = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    deal = new Deals();
                    deal.DEAL_ID = Convert.ToInt32(row["DEAL_ID"]);
                    deal.CATEGORY_ID = Convert.ToInt32(row["CATEGORY_ID"]);
                    deal.TOTAL_VIEW = Convert.ToInt32(row["TOTAL_VIEW"]);
                    deal.PUBLISH_DATE = DigiLib.DateTimeUtils.dd_MM_yyyy(Convert.ToDateTime(row["PUBLISH_DATE"]));
                    deal.DESCRIPTION = DigiLib.StringUtils.Substring(row["DESCRIPTION"].ToString(), 0, 60);
                    deal.SALE_PRICE = Convert.ToDecimal(row["SALE_PRICE"]).ToString("0,#,##");
                    deal.ORIGIN_PRICE = Convert.ToDecimal(row["ORIGIN_PRICE"]).ToString("0,#,##");
                    deal.DOWN_PRICE = Convert.ToDecimal(Convert.ToInt32(row["ORIGIN_PRICE"]) - Convert.ToInt32(row["SALE_PRICE"])).ToString("0,#,##");
                    deal.DOWN_PRICE_IN_PERCENT = Convert.ToDecimal(row["DOWN_PRICE_IN_PERCENT"]);
                    deal.THUMB_IMG = row["THUMB_IMG"].ToString();
                    deal.TITLE = row["TITLE"].ToString();
                    deal.LINK = "/khuyen-mai/" + DigiLib.StringUtils.RemoveUnicode(row["TITLE"].ToString(), "-") + "-c" + deal.CATEGORY_ID + "a" + deal.DEAL_ID + ".htm";
                    listDeals.Add(deal);
                }
            }
            else
            {
                listDeals = null;
            }
            return listDeals;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<Deals> GetLatestDeals(int rows)
    {
        try
        {
            List<Deals> listDeals = new List<Deals>();
            DataTable objDT = BO_portal_deals.GetLastest(rows);
            Deals deal = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    deal = new Deals();
                    deal.DEAL_ID = Convert.ToInt32(row["DEAL_ID"]);
                    deal.CATEGORY_ID = Convert.ToInt32(row["CATEGORY_ID"]);
                    deal.TOTAL_VIEW = 0;
                    deal.PUBLISH_DATE = string.Empty;
                    deal.DESCRIPTION = DigiLib.StringUtils.Substring(row["DESCRIPTION"].ToString(), 0, 60);
                    deal.SALE_PRICE = Convert.ToDecimal(row["SALE_PRICE"]).ToString("0,#,##");
                    deal.ORIGIN_PRICE = string.Empty;
                    deal.DOWN_PRICE = string.Empty;
                    deal.DOWN_PRICE_IN_PERCENT = Convert.ToDecimal(row["DOWN_PRICE_IN_PERCENT"]);
                    deal.THUMB_IMG = row["THUMB_IMG"].ToString();
                    deal.TITLE = DigiLib.StringUtils.Substring(row["TITLE"].ToString(), 0, 60);
                    deal.LINK = "/khuyen-mai/" + DigiLib.StringUtils.RemoveUnicode(row["TITLE"].ToString(), "-") + "-c" + deal.CATEGORY_ID + "a" + deal.DEAL_ID + ".htm";
                    listDeals.Add(deal);
                }
            }
            else
            {
                listDeals = null;
            }
            return listDeals;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<Deals> GetMostViewed(int rows)
    {
        try
        {
            List<Deals> listDeals = new List<Deals>();
            DataTable objDT = BO_portal_deals.GetMostViewed(rows);
            Deals deal = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    deal = new Deals();
                    deal.DEAL_ID = Convert.ToInt32(row["DEAL_ID"]);
                    deal.CATEGORY_ID = Convert.ToInt32(row["CATEGORY_ID"]);
                    deal.TOTAL_VIEW = 0;
                    deal.PUBLISH_DATE = string.Empty;
                    deal.DESCRIPTION = DigiLib.StringUtils.Substring(row["DESCRIPTION"].ToString(), 0, 60);
                    deal.SALE_PRICE = Convert.ToDecimal(row["SALE_PRICE"]).ToString("0,#,##");
                    deal.ORIGIN_PRICE = string.Empty;
                    deal.DOWN_PRICE = string.Empty;
                    deal.DOWN_PRICE_IN_PERCENT = Convert.ToDecimal(row["DOWN_PRICE_IN_PERCENT"]);
                    deal.THUMB_IMG = row["THUMB_IMG"].ToString();
                    deal.TITLE = DigiLib.StringUtils.Substring(row["TITLE"].ToString(), 0, 60);
                    deal.LINK = "/khuyen-mai/" + DigiLib.StringUtils.RemoveUnicode(row["TITLE"].ToString(), "-") + "-c" + deal.CATEGORY_ID + "a" + deal.DEAL_ID + ".htm";
                    listDeals.Add(deal);
                }
            }
            else
            {
                listDeals = null;
            }
            return listDeals;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<Deals> FilterDeals(string keyword, int category_id)
    {
        try
        {
            List<Deals> listDeals = new List<Deals>();
            DataTable objDT = BO_portal_deals.Filter(keyword, category_id, 1, 10);
            Deals deal = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    deal = new Deals();
                    deal.DEAL_ID = Convert.ToInt32(row["DEAL_ID"]);
                    deal.CATEGORY_ID = Convert.ToInt32(row["CATEGORY_ID"]);
                    deal.TOTAL_VIEW = 0;
                    deal.PUBLISH_DATE = string.Empty;
                    deal.DESCRIPTION = DigiLib.StringUtils.Substring(row["DESCRIPTION"].ToString(), 0, 60);
                    deal.SALE_PRICE = Convert.ToDecimal(row["SALE_PRICE"]).ToString("0,#,##");
                    deal.ORIGIN_PRICE = string.Empty;
                    deal.DOWN_PRICE = string.Empty;
                    deal.DOWN_PRICE_IN_PERCENT = Convert.ToDecimal(row["DOWN_PRICE_IN_PERCENT"]);
                    deal.THUMB_IMG = row["THUMB_IMG"].ToString();
                    deal.TITLE = DigiLib.StringUtils.Substring(row["TITLE"].ToString(), 0, 60);
                    deal.LINK = "/khuyen-mai/" + DigiLib.StringUtils.RemoveUnicode(row["TITLE"].ToString(), "-") + "-c" + deal.CATEGORY_ID + "a" + deal.DEAL_ID + ".htm";
                    listDeals.Add(deal);
                }
            }
            else
            {
                listDeals = null;
            }
            return listDeals;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public string MemberRegister(int region_id, string username, string password, string code, string avatar, string fullname, string email, string mobile, bool gender, string company, string address, DateTime birthday, string nick)
    {
        try
        {
            return BO_portal_members.Insert(region_id
                , code
                , ""
                , username
                , password
                , avatar
                , fullname
                , email
                , mobile
                , gender
                , company
                , address
                , birthday
                , nick
                , SqlDateTime.Null
                , SqlDateTime.Null
                , false
                , false
                , 0
                , 0
                , 0
                , 0
                , SqlDateTime.Null
                , SqlDateTime.Null
                , false
                , SqlDateTime.Null
                , string.Empty
                , string.Empty
                , SqlDateTime.Null
                , string.Empty
                , string.Empty
                , DateTime.Now
                , "MEMBER"
                , SqlDateTime.Null
                , string.Empty
                , 1);
        }
        catch
        {
            return "UNKNOW";
        }
    }

    [WebMethod(true)]
    public List<Members> SuccessLogin(string result)
    {
        string[] results = result.Split(new char[] { '-' });
        List<Members> listMB = new List<Members>();
        Members member = new Members();
        member.ID = results[0];
        member.USER_NAME = results[1];
        listMB.Add(member);
        Session.Add("Members", member);
        return listMB;
    }

    [WebMethod]
    public List<MessagesBox> ForgotPassword(string user_name, string email)
    {
        string result = BO_portal_members.ForgotPassword(user_name, email);
        List<MessagesBox> listMS = new List<MessagesBox>();
        MessagesBox message = new MessagesBox();
        if (string.IsNullOrEmpty(result))
        {
            message.CLASS = "notice_error";
            message.IMAGES = "error";
            message.TITLE = "Thông báo";
            message.DESCRIPTION = "Tài khoản hoặc email bạn nhập không chính xác.";
        }
        else
        {
            string fname = Server.MapPath(Setting.PathTemplateMail);
            XmlDocument doc = new XmlDocument();
            doc.Load(fname);
            XmlNode root = doc.DocumentElement;
            XmlNodeList list = root.ChildNodes;
            string body = Server.HtmlDecode(list.Item(0).InnerText);
            body = body.Replace("@PASSWORD", result);
            MailUtils mail = new MailUtils("RETHE.VN - Cấp lại mật khẩu", "[RETHE.VN]", body, email, true, true);
            if (mail.Send())
            {
                message.CLASS = "notice";
                message.IMAGES = "success";
                message.TITLE = "Thông báo";
                message.DESCRIPTION = "Chúng tôi đã gửi mật khẩu mới tới hòm thư: " + email + ". Quý khách vui lòng kiểm tra hòm thư.";
            }
        }
        listMS.Add(message);
        return listMS;
    }

    [WebMethod]
    public List<MessagesBox> LoginError(string result)
    {
        List<MessagesBox> listMS = new List<MessagesBox>();
        MessagesBox message = new MessagesBox();
        string[] results = result.Split(new char[] { '|' });
        message.CLASS = "notice_error";
        message.IMAGES = "error";
        message.TITLE = "Thông báo";
        switch (results[0])
        {
            case "INVALID_USERNAME":
                message.DESCRIPTION = "Tên người dùng bạn đã nhập không chính xác.";
                break;
            case "INVALID_PASSWORD":
                message.DESCRIPTION = "Mật khẩu bạn đã nhập không chính xác.";
                break;
            case "USER_LOCKED":
                if (results[1] != "")
                    message.DESCRIPTION = "Tài khoản của bạn đã bị khóa, lý do: \"" + results[1] + "\" .Hãy liên hệ với quản trị hệ thống để biết thông tin chi tiết!";
                else
                    message.DESCRIPTION = "Tài khoản của bạn đã bị khóa, .Hãy liên hệ với quản trị hệ thống để biết thông tin chi tiết!";
                break;
            default:
                break;
        }
        listMS.Add(message);
        return listMS;
    }

    [WebMethod]
    public List<DealType> GetAllDealType()
    {
        try
        {
            List<DealType> listDEALTYPE = new List<DealType>();
            DataTable objDT = BO_portal_deal_types.SelectAll(1);
            DealType dealtype = new DealType();
            dealtype.DEAL_TYPE_ID = "";
            dealtype.TITLE = "Hình thức khuyến mãi";
            listDEALTYPE.Add(dealtype);
            foreach (DataRow row in objDT.Rows)
            {
                dealtype = new DealType();
                dealtype.DEAL_TYPE_ID = row["DEAL_TYPE_ID"].ToString();
                dealtype.TITLE = row["TITLE"].ToString();
                listDEALTYPE.Add(dealtype);
            }
            return listDEALTYPE;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<Region> GetRegionByParent(int parentId)
    {
        try
        {
            Region region = new Region();
            List<Region> listREGION = new List<Region>();
            if (parentId == 0)
            {
                region.REGION_ID = "";
                region.REGION_NAME = "Chọn tỉnh/thành";
            }
            else
            {

                region.REGION_ID = "";
                region.REGION_NAME = "Chọn quận/huyện";
            }
            DataTable objDT = BO_portal_regions.SelectByParent(parentId);
            listREGION.Add(region);
            foreach (DataRow row in objDT.Rows)
            {
                region = new Region();
                region.REGION_ID = row["REGION_ID"].ToString();
                region.REGION_NAME = row["REGION_NAME_VN"].ToString();
                listREGION.Add(region);
            }
            return listREGION;
        }
        catch
        {
            return null;
        }
    }

    [WebMethod]
    public List<MessagesBox> ChangePassword(string username, string old_pass, string new_pass)
    {
        List<MessagesBox> listMS = new List<MessagesBox>();
        MessagesBox message = new MessagesBox();
        if (!BO_portal_members.ChangePassword(username, old_pass, new_pass).Equals("0"))
        {
            message.CLASS = "notice";
            message.IMAGES = "success";
            message.TITLE = "Thông báo";
            message.DESCRIPTION = "Mật khẩu của bạn đã được thay đổi.";
        }
        else
        {
            message.CLASS = "notice_error";
            message.IMAGES = "error";
            message.TITLE = "Thông báo";
            message.DESCRIPTION = "Mật khẩu truyền vào không chính xác.";
        }
        listMS.Add(message);
        return listMS;
    }

    [WebMethod]
    public List<MembersInfo> LoadInfo(string username)
    {
        List<MembersInfo> listMI = new List<MembersInfo>();
        MembersInfo member = new MembersInfo();
        DataTable objDT = BO_portal_members.Search(username);
        if (objDT.Rows.Count > 0)
        {
            DataRow objR = objDT.Rows[0];
            member.ADDRESS = objR["ADDRESS"].ToString();
            member.AVATAR_IMG = objR["AVATAR_IMG"].ToString();
            member.COMPANY = objR["COMPANY"].ToString();
            member.EMAIL = objR["EMAIL"].ToString();
            member.FULL_NAME = objR["FULL_NAME"].ToString();
            member.MOBILE = objR["MOBILE"].ToString();
            member.NICK_CHAT = objR["NICK_CHAT"].ToString();
            member.REGION_ID = objR["REGION_ID"].ToString();
            member.USER_NAME = objR["USER_NAME"].ToString();
        }
        listMI.Add(member);
        return listMI;
    }

    [WebMethod]
    public List<MessagesBox> ChangeInfo(string username, string fullname, string address, string tel, string mobile, string email, string yahoo, string skype, string website, string file, int region_id)
    {
        List<MessagesBox> listMS = new List<MessagesBox>();
        MessagesBox message = new MessagesBox();
        if (!BO_portal_members.Update(region_id, username, file, fullname, email, mobile, tel, address, yahoo, tel, website).Equals("0"))
        {
            message.CLASS = "notice";
            message.IMAGES = "success";
            message.TITLE = "Thông báo";
            message.DESCRIPTION = "Thông tin của bạn đã được thay đổi.";
        }
        else
        {
            message.CLASS = "notice_error";
            message.IMAGES = "error";
            message.TITLE = "Thông báo";
            message.DESCRIPTION = "Có lỗi trong quá trình sử lý. Vui lòng trở lại sau ýt phút";
        }
        listMS.Add(message);
        return listMS;
    }

    [WebMethod]
    public List<Comments> LoadComment(int start, string username)
    {
        List<Comments> listMI = new List<Comments>();
        Comments entity = new Comments();
        DataTable objDT = BO_portal_deals_comments.SelectByMember(start, 6, 1, username);
        foreach (DataRow objR in objDT.Rows)
        {
            entity.CREATED_DATE = Convert.ToDateTime(objR["CREATED_DATE"]).ToString("dd/MM/yyyy");
            entity.DETAIL = objR["DETAIL"].ToString();
            entity.FULL_NAME = objR["FULL_NAME"].ToString();
            entity.THUMB_IMG = objR["THUMB_IMG"].ToString();
            entity.CATEGORY_ID = objR["CATEGORY_ID"].ToString();
            entity.TOTAL_ROWS = objR["TOTAL_ROWS"].ToString();
            entity.DEAL_ID = objR["DEAL_ID"].ToString();
            entity.TITLE = DigiLib.StringUtils.Substring(objR["TITLE"].ToString(), 0, 60);
            entity.LINK = "/khuyen-mai/" + DigiLib.StringUtils.RemoveUnicode(objR["TITLE"].ToString(), "-") + "-c" + entity.CATEGORY_ID + "a" + entity.DEAL_ID + ".htm";
            listMI.Add(entity);
        }
        return listMI;
    }

    [WebMethod]
    public Members GetInfo(string user_name)
    {
        DataTable objDT = BO_portal_members.Search(user_name);
        //List<Members> listMS = new List<Members>();
        Members member = new Members();
        if (objDT.Rows.Count > 0)
        {
            member.ADDRESS = objDT.Rows[0]["ADDRESS"].ToString();
            member.AVATAR_IMG = objDT.Rows[0]["AVATAR_IMG"].ToString();
            member.EMAIL = objDT.Rows[0]["EMAIL"].ToString();
            member.FULL_NAME = objDT.Rows[0]["FULL_NAME"].ToString();
            member.GENDER = objDT.Rows[0]["GENDER"].ToString();
            member.ID = objDT.Rows[0]["MEMBER_ID"].ToString();
            member.MOBILE = objDT.Rows[0]["MOBILE"].ToString();
            member.NICK_CHAT = objDT.Rows[0]["NICK_CHAT"].ToString();
            member.TEL = objDT.Rows[0]["TEL"].ToString();
            member.USER_NAME = objDT.Rows[0]["USER_NAME"].ToString();
        }
        //listMS.Add(message);
        //return listMS;
        return member;
    }

    [WebMethod]
    public List<Deals> GetPersonDeal(string username, int start, int psize)
    {
        try
        {
            List<Deals> listDeals = new List<Deals>();
            DataTable objDT = BO_portal_deals.SelectByUser(username, start, psize);
            Deals deal = null;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    deal = new Deals();
                    deal.DEAL_ID = Convert.ToInt32(row["DEAL_ID"]);
                    deal.TOTAL_VIEW = Convert.ToInt32(row["TOTAL_VIEW"]);
                    deal.ROW = Convert.ToInt32(row["row"]);
                    deal.DEAL_CODE = row["DEAL_CODE"].ToString();
                    deal.TITLE = DigiLib.StringUtils.Substring(row["TITLE"].ToString(), 0, 55);
                    deal.TOOLTIP = row["TITLE"].ToString();
                    deal.TOTAL_ROWS = row["TOTAL_ROWS"].ToString();
                    deal.END_PUBLISH_DATE = Convert.ToDateTime(row["END_PUBLISH_DATE"]).ToString("dd/MM/yyyy");
                    deal.LINK = "/khuyen-mai/" + DigiLib.StringUtils.RemoveUnicode(row["TITLE"].ToString(), "-") + "-c" + deal.CATEGORY_ID + "a" + deal.DEAL_ID + ".htm";
                    listDeals.Add(deal);
                }
            }
            else
            {
                listDeals = null;
            }
            return listDeals;
        }
        catch
        {
            return null;
        }
    }
    [WebMethod]
    public Members LoadStatistic(string username)
    {
        Members member = new Members();
        DataTable objDT = BO_portal_members.Search(username);
        if (objDT.Rows.Count > 0)
        {
            DataRow objR = objDT.Rows[0];
            member.USER_NAME = objR["USER_NAME"].ToString();
            member.MEMBER_CODE = objR["MEMBER_CODE"].ToString();
            if (Convert.ToBoolean(objR["IS_VIP"]))
                member.IS_VIP = "Có";
            else
                member.IS_VIP = "Không";
            member.CREATED_DATE = Convert.ToDateTime(objR["CREATED_DATE"]).ToString("dd/MM/yyy");
            member.FIRST_LOGGED_IN = Convert.ToDateTime(objR["FIRST_LOGGED_IN"]).ToString("dd/MM/yyy");
            member.LAST_LOGGED_IN = Convert.ToDateTime(objR["LAST_LOGGED_IN"]).ToString("dd/MM/yyy");
            member.TOTAL_DEALS = objR["TOTAL_DEALS"].ToString();
            member.TOTAL_COMMENTS = objR["TOTAL_COMMENTS"].ToString();
        }
        return member;
    }

    [WebMethod]
    public MessagesBox DeleteDeal(string id)
    {
        int result = Convert.ToInt32(DigiLib.SqlUtils.ExecuteScalar("Delete From dbo.portal_deals WHERE DEAL_ID = " + id + " Select @@Rowcount;").ToString());
        MessagesBox message = new MessagesBox();
        if (result < 1)
        {
            message.CLASS = "notice_error";
            message.IMAGES = "error";
            message.TITLE = "Thông báo";
            message.DESCRIPTION = "Chưa thực hiện được.";
        }
        else
        {
            message.CLASS = "notice";
            message.IMAGES = "success";
            message.TITLE = "Thông báo";
            message.DESCRIPTION = "Thực hiện thành công.";
        }
        return message;
    }

    [WebMethod]
    public Deals LoadDeals(int id)
    {
        Deals deals = new Deals();
        DataTable objDT = BO_portal_deals.Search(id);
        if (objDT.Rows.Count > 0)
        {
            DataRow objR = objDT.Rows[0];
            deals.CATEGORY_ID = Convert.ToInt32(objR["CATEGORY_ID"]);
            //deals.DEAL_CODE = objR["DEALS_CODE"].ToString();
            deals.DEAL_ID = Convert.ToInt32(objR["DEAL_ID"]);
            deals.DESCRIPTION = objR["DESCRIPTION"].ToString();
            deals.DOWN_PRICE = objR["SALE_PRICE"].ToString();
            deals.DOWN_PRICE_IN_PERCENT = Convert.ToInt32(objR["DOWN_PRICE_IN_PERCENT"]);
            deals.END_PUBLISH_DATE = Convert.ToDateTime(objR["END_PUBLISH_DATE"]).ToString("MM/dd/yyyy");
            deals.ORIGIN_PRICE = objR["ORIGIN_PRICE"].ToString();
            deals.PUBLISH_DATE = Convert.ToDateTime(objR["PUBLISH_DATE"]).ToString("MM/dd/yyyy");
            deals.SALE_PRICE = objR["SALE_PRICE"].ToString();
            deals.THUMB_IMG = objR["THUMB_IMG"].ToString();
            deals.THUMB_IMG = deals.THUMB_IMG.Substring(deals.THUMB_IMG.LastIndexOf("/") + 1, deals.THUMB_IMG.Length - deals.THUMB_IMG.LastIndexOf("/") - 1);
            deals.TITLE = objR["TITLE"].ToString();
            deals.DEAL_TYPE_ID = Convert.ToInt32(objR["DEAL_TYPE_ID"]);
            deals.REGION_ID = Convert.ToInt32(objR["REGION_ID"]);
            deals.DEAL_DETAILS = objR["DEAL_DETAILS"].ToString();
        }
        return deals;
    }


}
